<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Role;
use App\Models\Menu;
use App\Models\Department;
use Illuminate\Support\Facades\Hash;
use Database\Seeders\PermissionSeeder;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // 创建默认部门
        $dept = Department::create([
            'parent_id' => 0,
            'name' => '总公司',
            'code' => 'ROOT',
            'status' => 1,
            'sort' => 0,
        ]);

        // 创建管理员角色
        $adminRole = Role::create([
            'name' => '管理员',
            'code' => 'admin',
            'status' => 1,
            'sort' => 0,
        ]);

        // 创建默认管理员用户
        $admin = User::create([
            'username' => 'admin',
            'nickname' => '管理员',
            'email' => 'admin@example.com',
            'password' => Hash::make('admin123'),
            'status' => 1,
            'dept_id' => $dept->id,
        ]);

        $admin->roles()->attach($adminRole->id);

        // 先运行权限种子，创建所有权限
        $this->call(PermissionSeeder::class);

        // 获取所有权限并分配给管理员角色
        $allPermissions = \App\Models\Permission::all();
        $adminRole->permissions()->sync($allPermissions->pluck('id')->toArray());

        // 创建Dashboard菜单（作为一级菜单目录）
        $dashboardMenu = Menu::create([
            'parent_id' => 0,
            'title' => '工作台',
            'name' => 'Dashboard',
            'path' => '/dashboard',
            'component' => 'Layout',
            'icon' => 'Odometer',
            'type' => 1,
            'status' => 1,
            'sort' => 0,
        ]);
        $adminRole->menus()->attach($dashboardMenu->id);

        // 创建Dashboard页面菜单项（使用相对路径，最终路径为 /dashboard）
        $dashboardPageMenu = Menu::create([
            'parent_id' => $dashboardMenu->id,
            'title' => '工作台',
            'name' => 'DashboardIndex',
            'path' => 'index',
            'component' => 'dashboard/index',
            'icon' => 'Odometer',
            'type' => 2,
            'status' => 1,
            'sort' => 1,
        ]);
        $adminRole->menus()->attach($dashboardPageMenu->id);

        // 创建默认菜单
        $systemMenu = Menu::create([
            'parent_id' => 0,
            'title' => '系统管理',
            'name' => 'System',
            'path' => '/system',
            'component' => 'Layout',
            'icon' => 'Setting',
            'type' => 1,
            'status' => 1,
            'sort' => 1,
        ]);
        $adminRole->menus()->attach($systemMenu->id);

        $menus = [
            [
                'parent_id' => $systemMenu->id,
                'title' => '用户管理',
                'name' => 'User',
                'path' => '/system/user',
                'component' => 'system/user/index',
                'icon' => 'User',
                'type' => 2,
                'permission' => 'users.view',
                'status' => 1,
                'sort' => 1,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '角色管理',
                'name' => 'Role',
                'path' => '/system/role',
                'component' => 'system/role/index',
                'icon' => 'UserFilled',
                'type' => 2,
                'permission' => 'roles.view',
                'status' => 1,
                'sort' => 2,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '菜单管理',
                'name' => 'Menu',
                'path' => '/system/menu',
                'component' => 'system/menu/index',
                'icon' => 'Menu',
                'type' => 2,
                'permission' => 'menus.view',
                'status' => 1,
                'sort' => 3,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '权限管理',
                'name' => 'Permission',
                'path' => '/system/permission',
                'component' => 'system/permission/index',
                'icon' => 'Key',
                'type' => 2,
                'permission' => 'permissions.view',
                'status' => 1,
                'sort' => 4,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '部门管理',
                'name' => 'Department',
                'path' => '/system/department',
                'component' => 'system/department/index',
                'icon' => 'OfficeBuilding',
                'type' => 2,
                'permission' => 'departments.view',
                'status' => 1,
                'sort' => 5,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '存储配置',
                'name' => 'Storage',
                'path' => '/system/storage',
                'component' => 'system/storage/index',
                'icon' => 'FolderOpened',
                'type' => 2,
                'status' => 1,
                'sort' => 6,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '操作日志',
                'name' => 'OperationLog',
                'path' => '/system/operation-log',
                'component' => 'system/operation-log/index',
                'icon' => 'Document',
                'type' => 2,
                'permission' => 'operation_logs.view',
                'status' => 1,
                'sort' => 7,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => 'WebSocket配置',
                'name' => 'WebSocketConfig',
                'path' => '/system/websocket',
                'component' => 'system/websocket/index',
                'icon' => 'Connection',
                'type' => 2,
                'status' => 1,
                'sort' => 8,
            ],
            [
                'parent_id' => $systemMenu->id,
                'title' => '环境变量配置',
                'name' => 'EnvConfig',
                'path' => '/system/env-config',
                'component' => 'system/env-config/index',
                'icon' => 'Tools',
                'type' => 2,
                'permission' => 'env_config.view',
                'status' => 1,
                'sort' => 9,
            ],
        ];

        $monitorMenu = Menu::create([
            'parent_id' => 0,
            'title' => '系统监控',
            'name' => 'Monitor',
            'path' => '/monitor',
            'component' => 'Layout',
            'icon' => 'Monitor',
            'type' => 1,
            'status' => 1,
            'sort' => 2,
        ]);
        $adminRole->menus()->attach($monitorMenu->id);

        $monitorMenus = [
            [
                'parent_id' => $monitorMenu->id,
                'title' => '在线用户',
                'name' => 'OnlineUser',
                'path' => '/monitor/online-user',
                'component' => 'monitor/online-user/index',
                'icon' => 'User',
                'type' => 2,
                'permission' => 'online_users.view',
                'status' => 1,
                'sort' => 1,
            ],
            [
                'parent_id' => $monitorMenu->id,
                'title' => '登录日志',
                'name' => 'LoginLog',
                'path' => '/monitor/login-log',
                'component' => 'monitor/login-log/index',
                'icon' => 'Document',
                'type' => 2,
                'permission' => 'login_logs.view',
                'status' => 1,
                'sort' => 2,
            ],
            [
                'parent_id' => $monitorMenu->id,
                'title' => '系统信息',
                'name' => 'SystemInfo',
                'path' => '/monitor/system',
                'component' => 'monitor/system/index',
                'icon' => 'InfoFilled',
                'type' => 2,
                'status' => 1,
                'sort' => 3,
            ],
        ];

        // 创建所有菜单并分配给管理员角色
        foreach ($menus as $menu) {
            $menuModel = Menu::create($menu);
            $adminRole->menus()->attach($menuModel->id);
        }

        foreach ($monitorMenus as $menu) {
            $menuModel = Menu::create($menu);
            $adminRole->menus()->attach($menuModel->id);
        }
    }
}

